@extends('layouts.colorbox')
@section('title', $title)
@section('content')
@section('style')
    <link href="../plugins/bootstrap-sweetalert/sweet-alert.css" rel="stylesheet" type="text/css">
@endsection
<div class="block-flat">
        <div class="content">
            <form action="{{ route('setting.role.permission', ['id'=>$role->id]) }}" method="post" id="role-permissions-form">
                <div class="panel-body panel-body-nopadding">
                    @foreach($permissions as $permission)
                        <div class="top-permission col-md-12">
                            <label>
                            @if(in_array($permission['id'],array_keys($rolePermissions)))
                                <input type="checkbox" name="permissions[]" value="{{ $permission['id'] }}"
                                       class="top-permission-checkbox" checked/>
                            @else
                                <input type="checkbox" name="permissions[]" value="{{ $permission['id'] }}"
                                       class="top-permission-checkbox"/>
                            @endif
                            &nbsp;{{ $permission['namespace']?:$permission['namespace'].'.'.$permission['controller']?:$permission['controller'].'.'.$permission['action']?:$permission['action'] }} - {{ $permission['name'] }}
                            </label>
                        </div>
                        @if(count($permission['subPermission']))
                            <div class="sub-permissions col-md-11 col-md-offset-1">
                                @foreach($permission['subPermission'] as $sub)
                                    <div class="col-sm-3">
                                        @if($sub['status'])
                                            <label>
                                                <input type="checkbox" name="permissions[]" value="{{ $sub['id'] }}" class="sub-permission-checkbox" {{ in_array($sub['id'], array_keys($rolePermissions)) ? 'checked':'' }}/>
                                                {{ $sub['namespace'].'.'.$sub['controller'].'.'.$sub['action'] }} {{ $sub['name'] ? ' - '.$sub['name'] : '' }} <span class="fa fa-eye"></span>
                                            </label>
                                        @else
                                            <label>
                                                <input type="checkbox" name="permissions[]" value="{{ $sub['id'] }}" class="sub-permission-checkbox" {{ in_array($sub['id'],array_keys($rolePermissions)) ? 'checked':'' }}/>
                                                {{ $sub['namespace'].'.'.$sub['controller'].'.'.$sub['action'] }} {{ $sub['name'] ? ' - '.$sub['name'] : '' }}
                                            </label>
                                        @endif
                                    </div>
                                @endforeach
                            </div>
                        @endif
                    @endforeach
                    {{ csrf_field() }}
                </div>
                <div class="panel-footer">
                    <div align="center">
                            <input type="submit" class="btn btn-primary" id="save-role-permissions" value="保存">
                    </div>
                </div><!-- panel-footer -->

            </form>
        </div>
</div>
@section('script')
    <script src="../plugins/bootstrap-sweetalert/sweet-alert.min.js"></script>
    <script src="{{ asset('js/ajax.js') }}"></script>
    <script>
        $(".top-permission-checkbox").change(function () {
            $(this).parents('.top-permission').next('.sub-permissions').find('input').prop('checked', $(this).prop('checked'));
        });
        $(".sub-permission-checkbox").change(function () {
            if ($(this).prop('checked')) {
                $(this).parents('.sub-permissions').prev('.top-permission').find('.top-permission-checkbox').prop('checked', true);
            }
        });
        $("#save-role-permissions").click(function (e) {
            e.preventDefault();
            Rbac.ajax.request({
                href: $("#role-permissions-form").attr('action'),
                data: $("#role-permissions-form").serialize(),
                successTitle: '角色权限保存成功'
            });
        });
    </script>
@endsection
@stop